package com.level2app.ui.search;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.Nullable;

import com.level2app.R;
import com.level2app.adapter.RecentSearchesAdapter;
import com.level2app.base.BaseActivity;
import com.level2app.databinding.ActivitySearchBinding;
import com.level2app.ui.search.view.SearchViewModel;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.KeyboardUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索页
 */
public class SearchActivity extends BaseActivity<ActivitySearchBinding, SearchViewModel> {

    private RecentSearchesAdapter adapter;
    private List<String> data = new ArrayList<>();

    @Override
    protected SearchViewModel bindModel() {
        return getViewModel(this, SearchViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.search_result));
        binding.setMViewModel(viewModel);
        data.add("");
        data.add("");
        data.add("");
        adapter = new RecentSearchesAdapter();
        binding.recyclerView.setAdapter(adapter);
        adapter.submitList(data);
    }

    @Override
    protected void initClick() {
        super.initClick();
        binding.mInputEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().length() > 0) {
                    binding.searchBg.setBackgroundResource(R.drawable.shape_stroke_000000_99dp);
                    binding.deleteIv.setVisibility(View.VISIBLE);
                } else {
                    binding.searchBg.setBackgroundResource(R.drawable.shape_stroke_dadada_99dp);
                    binding.deleteIv.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        viewModel.onDelayClick(binding.deleteIv, o -> binding.mInputEt.setText(""));

        // 按下回车跳转 SearchResultActivity
        binding.mInputEt.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                if (!TextUtils.isEmpty(binding.mInputEt.getText().toString().trim())) {
                    KeyboardUtils.hideSoftInput(SearchActivity.this);
                    Bundle bundle = new Bundle();
                    bundle.putString("search", binding.mInputEt.getText().toString().trim());
                    KeyboardUtils.hideSoftInput(this);
                    JumpIntent.jump(SearchActivity.this, SearchResultActivity.class, bundle, 1000);
                }
                return true;
            }
            return false;
        });

        adapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {
            KeyboardUtils.hideSoftInput(SearchActivity.this);
            JumpIntent.jump(SearchActivity.this, SearchResultActivity.class, 1000);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1000) {
                binding.mInputEt.requestFocus();
                KeyboardUtils.showSoftInput();
            }
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //返回具有焦点的当前视图
            View v = getCurrentFocus();
            if (KeyboardUtils.isShouldHideInput(v, ev)) {
                KeyboardUtils.hideSoftInput(this);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        KeyboardUtils.hideSoftInput(this);
    }
}